package cn.tedu.ivos.user.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.dto.UserQuery;
import cn.tedu.ivos.user.pojo.dto.UserSaveParam;
import cn.tedu.ivos.user.pojo.entity.User;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    //需要调用mapper
    @Autowired
    private UserMapper userMapper;

    @Override //重写父类中的方法
    public UserVO login(UserLoginParam userLoginParam) {
        log.debug("用户登录的参数为：{}",userLoginParam);
        UserVO userVO = userMapper.selectByUsername(userLoginParam.getUsername());
        //判断userVo是否为null
        if(userVO==null)
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        //判断controller传过来的密码和从数据库中获取的密码是否一致
        if(!userVO.getPassword().equals(userLoginParam.getPassword()))
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        //千万别忘记返回userVo
        return userVO;
    }

    @Override
    public void saveUser(UserSaveParam userSaveParam) {
        User user = new User();
        BeanUtils.copyProperties(userSaveParam,user);
        if(user.getId()==null){
            //新增用户时要判断该用户名是否存在
            if(userMapper.selectByUsername(user.getUsername())!=null){
                throw new ServiceException(StatusCode.USERNAME_ALREADY_EXISTS);
            }
            //注意:员工数据不是员工自己注册的，所以需要预设一个初始密码
            user.setPassword("123456");
            user.setCreateTime(new Date());
            userMapper.insert(user);
        }else{
            user.setUpdateTime(new Date());
            userMapper.update(user);
        }
    }

    @Override
    public List<UserVO> selectUser(UserQuery userQuery) {
        List<UserVO> list = userMapper.selectUser(userQuery);
        return list;
    }

    @Override
    public void resetPassword(Long userId) {
        User user = new User();
        user.setId(userId);
        user.setPassword("admin");
        userMapper.update(user);
    }

    @Override
    public void updateStatus(Long userId, String status) {
        userMapper.deleteById(userId);
    }

    @Override
    public List<UserVO> selectAuditList(Long parentId) {
        //准备一个集合，存放上级信息
        ArrayList<UserVO> userVOS = new ArrayList<>();
        //根据上级用户id查询上级用户的信息，添加到集合中
        UserVO auditUserVO1 = userMapper.selectById(parentId);
        userVOS.add(auditUserVO1);
        //判断auditUserVO1是否为null，不为null并且有parentID，再次进行查询
        if(auditUserVO1!=null && auditUserVO1.getParentId()!=null){
            //再次进行查询
            UserVO auditUserVO2 = userMapper.selectById(auditUserVO1.getParentId());
            userVOS.add(auditUserVO2);
        }
        //千万要记得返回信息
        return userVOS;
    }
}
